package com.anlu.thread.ta1;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: Thread-learning
 * @description:
 * @author: Mr.Wang
 * @create: 2019-01-30 14:55
 **/
public class Test {

    /**
     * 创建一个非公平锁，就是自由竞争锁
     */
    Lock lock = new ReentrantLock(false);

    public void a(){
        lock.lock();
        System.out.println(Thread.currentThread().getName()+" a");
        try{
            Thread.sleep(100);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        lock.unlock();
    }

    public static void main(String[] args){
        Test t = new Test();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    t.a();
                }
            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    t.a();
                }
            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    t.a();
                }
            }
        }).start();
    }
}
